﻿Ext.define('Gnt.examples.advanced.view.PlanTpl',
{
    extend: 'Ext.window.Window',
    modal: true,
    width: 600,
    height: 600,
    title: '计划模板',
    layout: 'fit',
    initComponent: function () {
        var me = this;
        me.items = [me.getTree()];
        me.buttons = [
            { text: '确定', handler: function () { me.save(); } },
            { text: '取消', handler: function () { me.close(); } }
        ];
        me.callParent(arguments);
    },
    getTree: function () {
        var me = this;
        if (!this.tree) {
            this.tree = new Ext.tree.Panel({
                border: false,
                useArrows: true,
                rootVisible: false,
                store: new Ext.data.TreeStore({
                    root: { expanded: true },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/plantpl/GetCheckTree'
                    }
                }),
                root: { id: '0', text: '' },
                listeners: {
                    afterrender: function (tree) { tree.expandAll(); },
                    checkchange: function (node, checked, eOpts) {
                        if (checked) {
                            node.eachChild(function (child) {
                                me.checkChild(child, true);
                            });
                        } else {
                            node.eachChild(function (child) {
                                me.checkChild(child, false);
                            });
                        }
                        me.checkParent(node);// 进行父级选中操作
                    }
                }
            });
        }
        return this.tree;
    },
    checkChild: function (node, check) {
        var me = this;
        node.set('checked', check);
        if (node.isNode) {
            node.eachChild(function (child) {
                me.checkChild(child, check);
            });
        }
    },
    nodep: function (node) {
        var bnode = true;
        Ext.Array.each(node.childNodes, function (v) {
            if (!v.data.checked) {
                bnode = false;
                return;
            }
        });
        return bnode;
    },
    checkParent: function (node) {
        var me = this;
        if (node.parentNode != null) {
            if (me.nodep(node.parentNode)) {
                node.parentNode.set('checked', true);
            } else {
                node.parentNode.set('checked', false);
            }
            me.checkParent(node.parentNode);
        }
    },
    save: function () {
        var me = this;
        var arry = me.getTree().getChecked();
        var plan = [];
        for (var i = 0; i < arry.length; i++) {
            var node = arry[i];
            plan.push({ Id: node.data.id, Name: node.data.text, Day: node.data.day, ParentId: node.data.parentId });
        }
        if (plan.length == 0) {
            Ext.Msg.alert('系统提示', "请选择模板任务");
            return false;
        }
        Ext.Ajax.request({
            url: '/api/services/app/plantpl/SetTplTask',
            params: { Plans: JSON.stringify(plan), ProjectId: _projectId },
            success: function (response) {
                me.fireEvent("savesuccess");
            }
        });
    }
});
